from flask import Blueprint, request, jsonify
import json
from utils.db import db1
from .compan import get_data

bp_message = Blueprint('message', __name__)


@bp_message.route('/add', methods=['GET', 'POST'])
def add():
    if request.method == 'POST':
        data = get_data()
        print(data)
        sql1 = """insert into message(title,content,stime,etime,priority,status,userid,type,userlist) values("%s","%s","%s","%s",%d,%d,%d,%d,"%s")""" % (
            data['title'], data['content'], data['stime'], data['etime'], int(data['priority']), int(data['status']),
            int(data['userid']), int(data['type']), data['userlist'])
        db1.update(sql1)
        print(sql1)
        return jsonify({'code': 200})
    elif request.method == 'GET':
        sql1 = """select m.*,u.name from message as m left join users as u on m.userid=u.id"""
        res1 = db1.findall(sql1)
        print(res1)

        return jsonify({'code': 200, 'list': res1})
